﻿#include "camera_qdialog.h"
#include "camera_hk.h"
#include <QDesktopWidget>

CameraHKQDialog::CameraHKQDialog(QDialog *parent, QSettings* qsettings,QString section) : QDialog(parent)
{
    QRect desktop_rect = QApplication::desktop()->geometry();
    double ratio_x = desktop_rect.width() / 1920.0;
    double ratio_y = desktop_rect.height() / 1080.0;

    move(0, 0);
	
    setFixedSize(1920 * ratio_x, 1010 * ratio_y);
    QGridLayout *qgridLayout = new QGridLayout(this);


    QVBoxLayout *qgridLayout_left = new QVBoxLayout();

	m_qsettings = qsettings;
	m_section = section;


    QSize control_size(120,40);

    m_qlabel_message = new QLabel();
    m_qlabel_message->setFixedSize(240,40);
    qgridLayout_left->addWidget(m_qlabel_message,1,0);

    m_qlabel_exposure_time = new QLabel();
    m_qlabel_exposure_time->setText("曝光时长");
    m_qlabel_exposure_time->setFixedSize(control_size);
    qgridLayout_left->addWidget(m_qlabel_exposure_time,1,0);

    m_qspinbox_exposure_time = new QSpinBox();
    m_qspinbox_exposure_time->setFixedSize(control_size);
    m_qspinbox_exposure_time->setMaximum(100000);
    qgridLayout_left->addWidget(m_qspinbox_exposure_time,2,0);

    m_qlabel_gain = new QLabel();
    m_qlabel_gain->setText("增益");
    m_qlabel_gain->setFixedSize(control_size);
    qgridLayout_left->addWidget(m_qlabel_gain,3,0);

    m_qspinbox_gain = new QSpinBox();
    m_qspinbox_gain->setFixedSize(control_size);
    m_qspinbox_gain->setMinimum(0);
    m_qspinbox_gain->setMaximum(1000);
    qgridLayout_left->addWidget(m_qspinbox_gain,4,0);

   /* m_qlabel_trigger_mode = new QLabel();
    m_qlabel_trigger_mode->setText("相机触发模式");
    m_qlabel_trigger_mode->setFixedSize(control_size);
    qgridLayout_left->addWidget(m_qlabel_trigger_mode,4,0);

    m_qcombox_trigger_mode = new QComboBox();
    m_qcombox_trigger_mode->setFixedSize(control_size);
    m_qcombox_trigger_mode->addItem("触发模式关闭");
    m_qcombox_trigger_mode->addItem("触发模式开启");
    qgridLayout_left->addWidget(m_qcombox_trigger_mode,5,0);*/



	
    /*m_qpushbutton_soft_triger = new QPushButton();
    m_qpushbutton_soft_triger->setFixedSize(control_size);
    m_qpushbutton_soft_triger->setText("软件触发");
    qgridLayout_left->addWidget(m_qpushbutton_soft_triger,6,0);
    connect(m_qpushbutton_soft_triger, &QPushButton::clicked, this, &CameraHKQDialog::softTrigger);*/


    m_qpushbutton_save = new QPushButton();
    m_qpushbutton_save->setFixedSize(control_size);
    m_qpushbutton_save->setText("保存参数");
    qgridLayout_left->addWidget(m_qpushbutton_save,7,0);
    connect(m_qpushbutton_save, &QPushButton::clicked, this, &CameraHKQDialog::saveParameter);

    m_qpushbutton_hide = new QPushButton();
    m_qpushbutton_hide->setFixedSize(control_size);
    m_qpushbutton_hide->setText("退出");
    qgridLayout_left->addWidget(m_qpushbutton_hide,8,0);


    qgridLayout->addLayout(qgridLayout_left,0,0);

	m_showqlable = new QtQGraphicsView(this);
	m_showqlable->setFixedSize(1000 * 1.25 * ratio_x, 700 * 1.25 * ratio_y);
	qgridLayout->addWidget(m_showqlable, 0, 1); 



    QTimer* m_qtimer = new QTimer(this);
    connect(m_qtimer, &QTimer::timeout, this, [=]() {
        m_bool_show_image = true;

        });
    m_qtimer->start(100);
}

bool CameraHKQDialog::initCamera(QString name,CameraCallback pfunction, void *pcontext)
{
//    logger->info("initCamera");

    std::string string_name = name.toLocal8Bit().toStdString();
    bool state = m_camera_interface->camera_init( (char*)string_name.c_str(),pfunction, pcontext);
    if(state == false)
    {
        QMessageBox::critical(nullptr, m_section, m_camera_name + "error");
        return false;
    }


    /*if(m_index!=0)
    {
        state = m_camera_interface->setTriggerSource();
        if(state == false)
        {
            QMessageBox::critical(nullptr, m_section, "setTriggerSource error");
            return false;
        }
    }*/
    loadParameter();
    return true;
}

void CameraHKQDialog::loadParameter()
{



        m_qspinbox_exposure_time->setValue(m_qsettings->value(m_section + "/" + "m_qspinbox_exposure_time", 1).toInt());
        m_qspinbox_gain->setValue(m_qsettings->value(m_section + "/" + "m_qspinbox_gain", 0).toInt());


        //m_qcombox_trigger_mode->setCurrentIndex(m_qsettings->value(m_section + "/" + "m_qcombox_trigger_mode", 0).toInt());


        bool state;

        state = m_camera_interface->setExposureTime(m_qspinbox_exposure_time->value());
        if(state==false)
        {
                QMessageBox::critical(nullptr, m_section, "ExposureTime");
                return;

        }


        state = m_camera_interface->setGainValue(m_qspinbox_gain->value());
        if(state==false)
        {
                QMessageBox::critical(nullptr, m_section, "Gain");
                return;
        }
        /*if(m_index!=0)
        {
            state = m_camera_interface->setTriggerMode(m_qcombox_trigger_mode->currentIndex());
            if(state==false)
            {
                QMessageBox::critical(nullptr, m_section, "setTriggerMode");
                return;
            }
        }*/




}

void CameraHKQDialog::saveParameter()
{
    if(m_camera_interface == nullptr)
    {
        return;
    }

    bool state;
    state = m_camera_interface->setExposureTime(m_qspinbox_exposure_time->value());
    if(state==false)
    {
        QMessageBox::critical(nullptr, m_section, "setExposureTime");
        return;
    }

    state = m_camera_interface->setGainValue(m_qspinbox_gain->value());
    if(state==false)
    {
        QMessageBox::critical(nullptr, m_section, "setGain");
        return;
    }

   /* if(m_index!=0)
    {
        state = m_camera_interface->setTriggerMode(m_qcombox_trigger_mode->currentIndex());
        if(state==false)
        {
            QMessageBox::critical(nullptr, m_section, "setTriggerMode");
            return;
        }
    }*/


	m_qsettings->setValue(m_section + "/" + "m_qspinbox_exposure_time", m_qspinbox_exposure_time->value());
	m_qsettings->setValue(m_section + "/" + "m_qspinbox_gain", m_qspinbox_gain->value());
    //m_qsettings->setValue(m_section + "/" + "m_qcombox_trigger_mode", m_qcombox_trigger_mode->currentIndex());

}

void CameraHKQDialog::showImage(Mat image,int camera_index,int index)
{
    if(m_bool_show_image==false)
    {
        return;
    }
    else
    {
        m_bool_show_image =false;
    }
    QString message = m_type + " " + m_camera_name  + "  "  + QString::number(index);
    QImage q_image = matToQImage(image);
    if (!q_image.isNull())
    {
        m_qlabel_message->setText(message);
      m_showqlable->setImage(q_image, message, Qt::green,20);
    }
}
void CameraHKQDialog::softTrigger()
{
    if(m_index==0)
        return;
    if(m_camera_interface == nullptr)
    {
        QMessageBox::critical(nullptr, m_section, "相机初始化失败");
        return;
    }
    bool state = m_camera_interface->softwareTrigger();
    if(state==false)
        QMessageBox::critical(nullptr, m_section, "softwareTrigger");
}


